<template>
  <div class="app-container">
    <div>
      <h2>暂无班级的学生信息</h2>
    </div>
    <p>请选选择学生，然后点击行政班级分班或教学班级分班</p>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          :disabled="multiple"
          @click="handleClazzOpen(0)"
        >行政班级分班</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="multiple"
          @click="handleClazzOpen(1)"
        >教学班级分班</el-button>
      </el-col>

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="studentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!--      <el-table-column label="学生" align="center" prop="sId" />-->
      <!--      <el-table-column label="学校" align="center" prop="schId" />-->
      <el-table-column label="学生姓名" align="center" prop="name" />
      <el-table-column label="行政班级" align="center" prop="clazzId" >
        <template slot-scope="scope">
          <span>{{ scope.row.clazzName != null ?  scope.row.clazzName : '-' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="教学班级" align="center" prop="clazzId2" >
        <template slot-scope="scope">
          <span>{{ scope.row.clazzName2 != null ?  scope.row.clazzName2 : '-' }}</span>
        </template>
      </el-table-column>

      <el-table-column label="学制" align="center" prop="sysName" >

      </el-table-column>
      <!--      <el-table-column label="院系" align="center" prop="deptId" />-->
      <!--      <el-table-column label="专业" align="center" prop="majorId" />-->
      <el-table-column label="年级" align="center" prop="sysGradeName" />

      <el-table-column label="入学时间" align="center" prop="entrance" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.entrance, '{y}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="年龄" align="center" prop="age" />
      <el-table-column label="性别" align="center" prop="gender">
        <template slot-scope="scope">
          <span>{{ scope.row.gender == 1 ? "男" : "女" }}</span>
        </template>
      </el-table-column>
      <el-table-column label="联系电话" align="center" prop="phone" />
      <el-table-column label="身份证号" align="center" prop="card" />

      <el-table-column label="户籍地址" align="center" prop="address1" />
      <el-table-column label="现居地址" align="center" prop="address2" />
<!--      <el-table-column label="缴费状态" align="center" prop="tuition" />-->
      <!--      <el-table-column label="打卡地址" align="center" prop="address3" />-->
      <el-table-column label="毕业学校" align="center" prop="school" />
<!--      <el-table-column label="出生日期" align="center" prop="birthday" />-->
<!--      <el-table-column label="父亲姓名" align="center" prop="parent1" />-->
<!--      <el-table-column label="父亲联系方式" align="center" prop="parent1Phone" />-->
<!--      <el-table-column label="父亲身份证号" align="center" prop="parent1Card" />-->
<!--      <el-table-column label="母亲姓名" align="center" prop="parent2" />-->
<!--      <el-table-column label="母亲联系方式" align="center" prop="parent2Phone" />-->
<!--      <el-table-column label="母亲身份证号" align="center" prop="parent2Card" />-->
<!--      <el-table-column label="qq" align="center" prop="qq" />-->
<!--      <el-table-column label="邮箱" align="center" prop="email" />-->
<!--      <el-table-column label="微信" align="center" prop="wechat" />-->

      <el-table-column label="状态" align="center" prop="status" />
      <el-table-column label="备注" align="center" prop="remark" />
<!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right">-->
<!--        <template slot-scope="scope">-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-edit"-->
<!--            @click="handleUpdate(scope.row)"-->
<!--            v-hasPermi="['teach:student:edit']"-->
<!--          >修改</el-button>-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--            v-hasPermi="['teach:student:remove']"-->
<!--          >删除</el-button>-->
<!--        </template>-->
<!--      </el-table-column>-->
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改学生对话框 -->
<!--    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>-->
<!--      <el-form ref="form" :model="form" :rules="rules" label-width="80px">-->
<!--        <el-form-item label="行政班级" prop="clazzId">-->
<!--          <el-select v-model="form.clazzId" clearable placeholder="请选择">-->
<!--          <el-option-->
<!--            v-for="item in ClazzList"-->
<!--            :key="item.cId"-->
<!--            :label="item.name"-->
<!--            :value="item.cId">-->
<!--          </el-option>-->
<!--        </el-select>-->
<!--        </el-form-item>-->
<!--      </el-form>-->
<!--      <div slot="footer" class="dialog-footer">-->
<!--        <el-button type="primary" @click="submitForm">确 定</el-button>-->
<!--        <el-button @click="cancel">取 消</el-button>-->
<!--      </div>-->
<!--    </el-dialog>-->
    <!-- 学生导入对话框 -->
<!--    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>-->
<!--      <el-upload-->
<!--        ref="upload"-->
<!--        :limit="1"-->
<!--        accept=".xlsx, .xls"-->
<!--        :headers="upload.headers"-->
<!--        :action="upload.url + '?updateSupport=' + upload.updateSupport"-->
<!--        :disabled="upload.isUploading"-->
<!--        :on-progress="handleFileUploadProgress"-->
<!--        :on-success="handleFileSuccess"-->
<!--        :auto-upload="false"-->
<!--        drag-->
<!--      >-->
<!--        <i class="el-icon-upload"></i>-->
<!--        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>-->
<!--        <div class="el-upload__tip text-center" slot="tip">-->
<!--          <div class="el-upload__tip" slot="tip">-->
<!--            <el-checkbox v-model="upload.updateSupport" /> 是否更新已经存在的用户数据-->
<!--          </div>-->
<!--          <span>仅允许导入xls、xlsx格式文件。</span>-->
<!--          <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;" @click="importTemplate">下载模板</el-link>-->
<!--        </div>-->
<!--      </el-upload>-->
<!--      <div slot="footer" class="dialog-footer">-->
<!--        <el-button type="primary" @click="submitFileForm">确 定</el-button>-->
<!--        <el-button @click="upload.open = false">取 消</el-button>-->
<!--      </div>-->
<!--    </el-dialog>-->
    <el-dialog :title="'请选择班级'" :visible.sync="clazzOpen" width="400px" append-to-body>
      <div>
        <el-select v-model="clazzId" placeholder="请选择">
          <el-option :value="0"  :label="'请选择学生班级'" ></el-option>
          <el-option
            v-for="item in ClazzList"
            :key="item.cId"
            :label="item.name"
            :value="item.cId">
          </el-option>
        </el-select>

      </div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitClazz">确 定</el-button>
        <el-button @click="clazzOpen = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listStudent, getStudent, delStudent, addStudent, updateStudent, listFStudent } from "@/api/teach/student";
import { listClazz} from "@/api/teach/clazz";
import {getToken} from "@/utils/auth";

export default {
  name: "Student",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 学生表格数据
      studentList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        schId: null,
        clazzId: null,
        clazzId2: null,
        sysId: null,
        deptId: null,
        majorId: null,
        gradeId: null,
        name: null,
        entrance: null,
        phone: null,
        qq: null,
        email: null,
        wechat: null,
        address1: null,
        address2: null,
        address3: null,
        school: null,
        age: null,
        gender: null,
        card: null,
        birthday: null,
        parent1: null,
        parent1Phone: null,
        parent1Card: null,
        parent2: null,
        parent2Phone: null,
        parent2Card: null,
        tuition: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      queryClazz :{
        category: 0
      },
      //行政班级列表
      ClazzList:[],
      // 学生导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/teach/student/importData"
      },
      //行政班级条件
      clazzOpen: false,
      clazzId: 0,
      actionType: 0
    };
  },
  created() {
    this.getList();

  },
  methods: {
    /** 查询学生列表 */
    getList() {
      this.loading = true;
      this.queryParams.clazzId = -1

      this.queryParams.clazzId2 = -1
      listFStudent(this.queryParams).then(response => {
        this.studentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        sId: null,
        schId: null,
        clazzId: null,
        clazzId2: null,
        sysId: null,
        deptId: null,
        majorId: null,
        gradeId: null,
        name: null,
        entrance: null,
        phone: null,
        qq: null,
        email: null,
        wechat: null,
        address1: null,
        address2: null,
        address3: null,
        school: null,
        age: null,
        gender: null,
        card: null,
        birthday: null,
        parent1: null,
        parent1Phone: null,
        parent1Card: null,
        parent2: null,
        parent2Phone: null,
        parent2Card: null,
        tuition: null,
        status: 0,
        createBy: null,
        createTime: null,
        updateBy: null,
        delFlag: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 查询行政班级 */
    getClazz() {
      listClazz({category: this.actionType}).then(response =>{
        this.ClazzList = response.rows;
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.sId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      console.log(this.ids);
      this.open = true;
      this.title = "添加学生";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const sId = row.sId || this.ids
      getStudent(sId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改学生";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.sId != null) {
            updateStudent(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addStudent(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const sIds = row.sId || this.ids;
      this.$modal.confirm('是否确认删除学生编号为"' + sIds + '"的数据项？').then(function() {
        return delStudent(sIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('teach/student/export', {
        ...this.queryParams
      }, `student_${new Date().getTime()}.xlsx`)
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "用户导入";
      this.upload.open = true;
    },
    /** 下载模板操作 */
    importTemplate() {
      this.download('teach/student/importTemplate', {
      }, `student_template_${new Date().getTime()}.xlsx`)
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert(response.msg, "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },
    // 查询班级数据
    handleClazzOpen(t) {
      this.clazzOpen = true;
      this.actionType = t;
      this.getClazz();
    },
    //分班方法提交，暂时循环提交
    submitClazz(){
      this.ids.forEach(id => {
        let d = {sId: id}
        if(this.actionType == 0) {
          // 行政班级
          d.clazzId = this.clazzId;
        }else{
          // 教学班级
          d.clazzId2 = this.clazzId;
        }

        updateStudent(d).then(response => {
          // this.$modal.msgSuccess("修改成功");
          // this.open = false;
          //
        });

      });
      this.getList();
      this.clazzOpen = false;
    }
  }
};
</script>
